Imports System.Collections.Generic
Imports System.Data
Imports System.Data.Entity
Imports System.Linq

Public Class <OwnerU>_<TableU>DataClass

    Public Function SelectAll() As DataTable
        Using context = New <Database>DBContext()
            Return context.<OwnerU>_<TableU>.ToList().ToDataTable()
        End Using
    End Function

    Public Function Select_Record(_<OwnerU>_<TableU> As <OwnerU>_<TableU>) As <OwnerU>_<TableU>
        Using context = New <Database>DBContext()
            Return context.<OwnerU>_<TableU>.Find(
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                        _<OwnerU>_<TableU>.<FieldU> _
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                        , _<OwnerU>_<TableU>.<FieldU> _
    </If Loop>
                                        )
        End Using
    End Function

    Public Function Add(_<OwnerU>_<TableU> As <OwnerU>_<TableU>) As Boolean
        Using context = New <Database>DBContext()
            context.<OwnerU>_<TableU>.Add(_<OwnerU>_<TableU>)
            Return context.SaveChanges() > 0
        End Using
    End Function

    Public Function Update(_<OwnerU>_<TableU> As <OwnerU>_<TableU>) As Boolean
        Using context = New <Database>DBContext()
            context.<OwnerU>_<TableU>.Attach(_<OwnerU>_<TableU>)
            context.Entry(_<OwnerU>_<TableU>).State = System.Data.Entity.EntityState.Modified
            Return context.SaveChanges() > 0
        End Using
    End Function

    Public Function Delete(_<OwnerU>_<TableU> As <OwnerU>_<TableU>) As Boolean
        Using context = New <Database>DBContext()
            context.<OwnerU>_<TableU>.Remove(context.<OwnerU>_<TableU>.Find(
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                        _<OwnerU>_<TableU>.<FieldU> _
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                        , _<OwnerU>_<TableU>.<FieldU> _
    </If Loop>
                                        ))
            Return context.SaveChanges() > 0
        End Using
    End Function

End Class
